//
//  ViewController.m
//  FileManager
//
//  Created by 刘飞 on 2018/10/26.
//  Copyright © 2018 刘飞. All rights reserved.
//

#import "ViewController.h"
#import "HWPDFBrowseVC.h"
#import <WebKit/WebKit.h>
#import "WKWebViewController.h"

@interface ViewController ()<WKNavigationDelegate, WKUIDelegate>

@end

@implementation ViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    

//    [self getFilePath];
}

#pragma mark - 获取文件名称
- (void)getFilePath {
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docuPath = [paths firstObject];
    NSLog(@"documentPath ========= %@", docuPath);
    
    NSFileManager *fm = [NSFileManager defaultManager];
    NSString *imagePath = [NSString stringWithFormat:@"%@/%@", docuPath, @"images"];
    NSArray *images = [fm subpathsAtPath:imagePath];
    NSLog(@"imagepath = %@ \n imagesssss = %@", imagePath, images);
    
    NSDictionary *attr = @{@"NSFileExtensionHidden": @"0"};
    NSError *error = nil;
    BOOL isset = [fm setAttributes:attr ofItemAtPath:[NSString stringWithFormat:@"%@/%@", imagePath, @"1024.png"] error:&error];
    NSLog(@"isset -------------%d", isset);
    for (NSString *path in images) {
        
        NSString *temp = [NSString stringWithFormat:@"%@/%@", imagePath, path];
        NSDictionary *dic = [fm attributesOfItemAtPath:temp error:nil];
        NSLog(@"diccccc +++++++++ %@", dic);
        
        NSString *tempPath = [path stringByDeletingPathExtension];
        NSLog(@"temp ++++++++ %@", tempPath);
        
    }
}


//- (BOOL)isiPhoneX {
//    static BOOL isiPhoneX = NO;
//    static dispatch_once_t onceToken;
//    dispatch_once(&onceToken, ^{
//
//#if TARGET_IPHONE_SIMULATOR
//        // 获取模拟器所对应的 device model
//        NSString *model = NSProcessInfo.processInfo.environment[@"SIMULATOR_MODEL_IDENTIFIER"];
//#else
//        // 获取真机设备的 device model
//        struct utsname systemInfo;
//        uname(&systemInfo);
//        NSString *model = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
//#endif
//        // 判断 device model 是否为 "iPhone10,3" 和 "iPhone10,6" 或者以 "iPhone11," 开头
//        // 如果是，就认为是 iPhone X
//        isiPhoneX = [model isEqualToString:@"iPhone10,3"] || [model isEqualToString:@"iPhone10,6"] || [model hasPrefix:@"iPhone11,"];
//    });
//
//    return isiPhoneX;
//}


- (IBAction)showPDFAction:(id)sender {
    
    NSLog(@"pdf");
    HWPDFBrowseVC *vc = [[HWPDFBrowseVC alloc] init];
    vc.filePath = [[NSBundle mainBundle] pathForResource:@"HWTest.pdf" ofType:nil];
    vc.fileName = @"HWTestPDF";
//    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
//    [self presentViewController:nav animated:YES completion:nil];
    [self.navigationController pushViewController:vc animated:YES];
}

- (IBAction)showWordAction:(id)sender {
    
    UIAlertController *al = [UIAlertController alertControllerWithTitle:@"" message:@"选择要查看的文件类型：" preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"docx" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        [self webviewWithUrl:@"wordfile" type:@"docx"];
    }];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"ppt" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        [self webviewWithUrl:@"pptfile" type:@"ppt"];
    }];
    UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"xls" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        [self webviewWithUrl:@"xlsfile" type:@"xls"];
    }];
    UIAlertAction *action4 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [al addAction:action1];
    [al addAction:action2];
    [al addAction:action3];
    [al addAction:action4];
    [self presentViewController:al animated:YES completion:nil];
    
    
}

- (void)webviewWithUrl:(NSString *)name type:(NSString *)type {
    NSString *filePath = [[NSBundle mainBundle] pathForResource:name ofType:type];
    UIWebView *webview = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    [webview loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:filePath]]];
    [webview setScalesPageToFit:YES];
    [self.view addSubview:webview];
}

- (IBAction)openWebview:(id)sender {
    
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    WKWebViewController *vc = [sb instantiateViewControllerWithIdentifier:@"webviewVC"];
    NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.baidu.com"]];
    vc.request = req;
    [self.navigationController pushViewController:vc animated:YES];
}

- (IBAction)tucaoAction:(id)sender {
    
    NSString *openid = @"syngenta123";
    NSString *nickname = @"syngenta";
    NSString *avatar = @"https://tucao.qq.com/static/desktop/img/products/def-product-logo.png";
    
    NSString *app_url = [NSString stringWithFormat:@"%@%@", @"https://support.qq.com/product/", @"45130"];
    NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:app_url]];
    // 设置请求类型为post
    [req setHTTPMethod:@"POST"];
    [req setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    // 设置请求体
    NSString *body = [NSString stringWithFormat:@"nickname=%@&avatar=%@&openid=%@", nickname, avatar, openid];
    [req setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
    
    NSLog(@"appurl = %@", app_url);
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    WKWebViewController *vc = [sb instantiateViewControllerWithIdentifier:@"webviewVC"];
    vc.request = req;
    [self.navigationController pushViewController:vc animated:YES];

}


@end
